<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> 叫餐  <{$smarty.const.WEB_NAME}></title>
	<link rel="stylesheet" type="text/css" href="./skin/default/style.css" />
	<link rel="stylesheet" type="text/css" href="./skin/default/winBox.css" />
	<script type="text/javascript" src="./skin/default/showDialog.js"></script> 
	<style type="text/css">
	     .dataList	    { margin-left:1px; *margin-left:0; _margin-left:0; }	
	     .dataList th	{ background:#F2F6F9; line-height:30px; color:#555}	
	     .dataList td	{ border-bottom:2px solid #F2F6F9; line-height:30px ;text-align:center;}	
		 .resultBox     {border:solid 1px #C4D3EA; background-color:#EEF3F8; width:500px; padding: 1px 2px; margin: 5px auto}
		 .resultBox h2	{font-size:14px; text-indent:20px;padding-top:3px}
		 .resultBox span{color:#058EC4;font-weight:900;margin-left:3px;}
		 .resultBox table{ border-collapse:collapse;background-color:#FFECC6 }
		 .resultBox td  {padding-left:3px;}
		  select		{font-size:13px;}
	</style>	
</head>
 <body>
	 
        <{include file="#header.html"}>
	
     <div class="box">
	    <div class="title">
		   <div class="left" style="width:300px; padding-top:20px;padding-left:20px;">
			 <input type="button" value="叫餐" onclick="location.hreff = 'index.php?q=todayMenu'" />&nbsp;&nbsp;
			 <input type="button" value="商家管理" onclick="location.href = 'index.php?q=mealType'" />
		   </div>
		   <div class="right"></div>
		</div>
		<div class="list">

  <div class="list">
	    <table width="500" border="0" cellspacing="0" cellpadding="0" class="dataList" align="center">
	      <tr>
	        <th>姓名</th>
	        <th>吃什么</th>
	        <th>价格</th>
	        <th>选择者</th>
	        <th>发生日期</th>
	      </tr>
	   <{section name=list loop=$user}>

		  <tr id="user_<{$user[list].id}>">
		    <td><{$user[list].name}></td>
		    <td fid="<{$user[list].id}>">
				<{$select}>
			</td>
		<{assign var="isSet" value="0"}>
		 <{section name=child loop=$list}>
		    <{if $user[list].id == $list[child].userId}>
			  <{assign var="isSet" value="1"}>		
			  <td>￥<{$list[child].price}></td> 
			  <td>&nbsp;<{$list[child].userName}></td> 			  
			  <td>&nbsp;<{$list[child].time}></td> 
			<{/if}>
		 <{/section}>
	     <{if $isSet == 0}>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		 <{/if}>		   
		  </tr>
		 <{assign var="isSet" value="0"}>
		<{/section}>

	    </table> 
	 </div>

     <div class="resultBox" style="display:none">
	   <h2>统计结果</h2>
	   <table width="98%" align="center" border="1" cellspacing="0" cellpadding="0" bordercolor="#FFCC66" id="totalResult">
	   </table>
	 </div>

		</div>
	    <div class="bottom">
		   <div class="left"></div>
		   <div class="right"></div>		
		</div>	 
	 </div>
	 <script type="text/javascript">
	 <!--
		var userKey = '<{$userKey}>';
		var mealKey = '<{$mealKey}>';
		var idKey = '<{$idKey}>';
		var parentName = '<{$parentName}>';
		var parentKey = [<{$parentKey}>];
		var dataList = new Array();
<{section name=c loop=$list}>
  dataList[<{$smarty.section.c.index}>] = ['<{$list[c].name}>','<{$list[c].price}>','<{$list[c].parentId}>'];
<{/section}>
		function init(){
		   try{
			if(userKey != "" && mealKey != ""){
			   var userList = userKey.split(",");
			   var mealList = mealKey.split(",");
			   var idList = idKey.split(",");
			   for(var i = 0; i < userList.length; i++){
			      var obj = get('user_'+userList[i]);
				  if(null != obj && null != mealList[i] && null != idList[i]){
				    obj = obj.getElementsByTagName('select')[0];
				    obj.value = mealList[i];
					obj.lock = idList[i];
				  }
			   }
			   
			}
		  }catch(e){
		    window.status = 'err=' +e;
		  }
		}

		function makeArray(){
		   try{
			   var pName = parentName.split(',');
			   var parentList = new Array();
			   for(var i = 0 ; i < pName.length; i++){
				  parentList[parentKey[i]] = pName[i];
			   }
			   return parentList;		   
		   } catch(e){
		     window.status = 'error ='+e;
		   }
		   return null;
		}

		function check(data, checkItem){
			var index = -1; 
			var temp = '';
			for (var i=0; i<data.length; i++){
				temp  = typeof(data[i]) == 'string' ? data[i] : data[i][0];
				if (temp ==checkItem){
					index = i;
					break;
				}
			}
			return index;
		}

		function countData(data , index){
		  var list = new Array();
		  var j = -1;
		  for(var i = 0 ; i < data.length; i++){
		    if(data[i][2] == index){
				if(list.length == 0){
				  list[++j] = new Array();
				  list[j][0] = data[i][0];
				  list[j][2] = parseInt(data[i][1]);
				  list[j][1] = 1;
				  continue;
				}
				if(check(list , data[i][0]) == -1){
				   list[++j] = new Array();
				   list[j][0] = data[i][0];
				   list[j][2] = parseInt(data[i][1]);
				   list[j][1] = 1;
				}else{
				  for(var k = 0; k< list.length; k++){
				     if(list[k][0] == data[i][0]){
					    list[k][2] += parseInt(data[i][1]);
						list[k][1] ++;
						break;
					 }
				  }
				}	
			}
		  }
		  return list;;
		}

		function formatText(data){
		  var txt = '';
		  for(var i = 0; i < data.length; i++){
		     txt += data[i][0]+'<span>'+data[i][1]+'</span> ,';
		  }
		  return txt;
		}

		function totalPrice(data){
		   var n = 0;
		   for(var i=0; i < data.length; i++){
		     n += parseInt(data[i][2]);
		   }
		   return n;
		}

		function distinctTypes(){
		   var list = new Array();
		   for(var i = 0; i < dataList.length; i++){
		      if(check(list ,dataList[i][2]) == -1){
				list.push(dataList[i][2]);
			  }			  
		   }
		   return list;
		}
	
		function toCalculation(){
		
		   var list = makeArray();
		   var types = distinctTypes();
		   var obj = get('totalResult');
		   obj.parentNode.style.display = 'block';
		   try{
			   for(var j = 0; j < types.length; j++){
				  var result = countData(dataList , types[j]);
				  var tr = obj.insertRow(obj.rows.length); 
				  var td = tr.insertCell(0);
				  td.innerHTML = types[j];
				  td.innerHTML = list[types[j]];
				  td = tr.insertCell(1);
				  td.innerHTML = formatText(result);
				  td = tr.insertCell(2);
				  td.innerHTML = totalPrice(result);	
			   }		   
		   }catch(e){
		     alert('error='+e);
		   }
		}

		function toType(obj){
		   var uid = obj.parentNode.getAttribute("fid");
		   if(obj.lock  > 0 ){
			 location.href = 'index.php?q=editMenu&mealId='+obj.value+'&id='+obj.lock+'&uid='+uid;
		   }else{
			 location.href = 'index.php?q=addMenu&mealId='+obj.value+'&uid='+uid;
		   }
		}
	init();
	toCalculation();
	 //-->
	 </script>
	 
 </body>
</html>
